package com.tensquare.spit.controller;

import com.tensquare.spit.pojo.Spit;
import com.tensquare.spit.service.SpitService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 吐槽Controller.
 *
 * @author 宫新程
 * @since 2019/3/3 23:57
 */
@CrossOrigin
@RestController
@RequestMapping("/spit")
public class SpitController {

  @Resource private SpitService spitService;
  @Resource private RedisTemplate redisTemplate;

  @GetMapping
  public Result findAll() {
    List<Spit> result = spitService.findAll();
    return new Result(true, StatusCode.OK, "查询成功", result);
  }

  @GetMapping("/{spitId}")
  public Result findById(@PathVariable String spitId) {
    Spit spit = spitService.findById(spitId);
    return new Result(true, StatusCode.OK, "查询成功", spit);
  }

  /** 增加吐槽. */
  @PostMapping
  public Result save(@RequestBody Spit spit) {
    spitService.save(spit);
    return new Result(true, StatusCode.OK, "添加成功");
  }

  /** 更新. */
  @PutMapping("/{spitId}")
  public Result update(@RequestBody Spit spit, @PathVariable String spitId) {
    spit.set_id(spitId);
    spitService.update(spit);
    return new Result(true, StatusCode.OK, "修改成功");
  }

  /** 删除. */
  @DeleteMapping("/{spitId}")
  public Result delete(@PathVariable String spitId) {
    spitService.delete(spitId);
    return new Result(true, StatusCode.OK, "删除成功");
  }

  /** 根据上级ID查询吐槽数据（分页）. */
  @GetMapping("/comment/{parentid}/{page}/{size}")
  public Result findByParentId(
      @PathVariable String parentid, @PathVariable int page, @PathVariable int size) {
    Page<Spit> pageData = spitService.findByParentId(parentid, page, size);
    return new Result(
        true,
        StatusCode.OK,
        "查询成功",
        new PageResult<Spit>(pageData.getTotalElements(), pageData.getContent()));
  }

  /** 点赞. */
  @PutMapping("/thumbup/{spitId}")
  public Result thumbup(@PathVariable String spitId) {
    // TODO：由于没有做用户认证，暂时把 userId 写死
    String userId = "111";
    Object obj = redisTemplate.opsForValue().get("spit_thumbup_" + spitId + "_" + userId);
    if (obj != null) {
      return new Result(false, StatusCode.ERROR, "点赞失败");
    }
    redisTemplate.opsForValue().set("spit_thumbup_" + spitId + "_" + userId, 1);
    spitService.thumbup(spitId);
    return new Result(true, StatusCode.OK, "点赞成功");
  }
}
